package com.voxel.sense.infomsg.view.titlebar;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.utils.ResourceUtil;

public abstract class BaseTitleBar extends LinearLayout {
    private ImageButton ib_right_more;
    private TextView tv_title, tv_left;
    View title_divider;
    private int buttonSize = 0;

    @SuppressLint("NewApi")
    public BaseTitleBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public BaseTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BaseTitleBar(Context context) {
        super(context);
    }

    protected void initBaseView(final Context context, AttributeSet attrs) {
        setOrientation(LinearLayout.VERTICAL);
        buttonSize = ResourceUtil.getDimenPixelSize(context,
                R.dimen.titlebar_btn_width);
        tv_left = (TextView) findViewById(R.id.titlebar_tv_left);
        title_divider = findViewById(R.id.title_divider);
        ib_right_more = (ImageButton) findViewById(R.id.titlebar_ibtn_right_more);
        tv_left.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (context instanceof Activity) {
                    ((Activity) context).onBackPressed();
                }
            }
        });
        tv_title = (TextView) findViewById(R.id.titlebar_tv_title);
        int textSize = ResourceUtil.getDimensionNumber(context,
                R.dimen.titlebar_title_textsize);
        tv_title.setTextSize(textSize);
        if (attrs != null && attrs.getAttributeCount() > 0) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs,
                    R.styleable.TitleBar);
            if (typedArray != null) {
                int count = typedArray.getIndexCount();
                String titleText = null;
                for (int i = 0; i < count; i++) {
                    int attr = typedArray.getIndex(i);
                    switch (attr) {
                        case R.styleable.TitleBar_title_text:
                            titleText = typedArray.getString(attr);
                            break;
                        case R.styleable.TitleBar_left_visibile:
                            boolean leftVisibile = typedArray.getBoolean(attr, true);
                            tv_left.setVisibility(leftVisibile ? View.VISIBLE : View.INVISIBLE);
                            break;
                        default:
                            break;
                    }
                }
                typedArray.recycle();
                if (!TextUtils.isEmpty(titleText)) {
                    tv_title.setText(titleText);
                }
            }
        }

    }

    /**
     * 设置标题文本
     *
     * @param text
     */
    public BaseTitleBar setTitleText(CharSequence text) {
        tv_title.setText(text);
        return this;
    }

    /**
     * 设置标题文本
     *
     * @param resid
     */
    public BaseTitleBar setTitleText(int resid) {
        tv_title.setText(resid);
        return this;
    }

    public BaseTitleBar setTitleTextColor(int color) {
        tv_title.setTextColor(color);
        return this;
    }

    /**
     * 获取标题
     *
     * @return title
     */
    public String getTitleText() {
        return tv_title.getText().toString();
    }


    /**
     * 设置左边按扭图标和监听
     */
    public BaseTitleBar setLeftImgAndLis(int resid, OnClickListener lis) {
        tv_left.setOnClickListener(lis);
        tv_left.setCompoundDrawablesWithIntrinsicBounds(resid, 0, 0, 0);
        return this;
    }

    /**
     * 设置左边按扭监听,默认是返回图标
     */
    public BaseTitleBar setLeftLis(OnClickListener lis) {
        tv_left.setOnClickListener(lis);
        return this;
    }

    /**
     * 设置右边更多按扭图片与监听
     */
    public BaseTitleBar setRightMoreImgAndLis(int resid, OnClickListener lis) {
        ib_right_more.setImageResource(resid);
        ib_right_more.setOnClickListener(lis);
        ib_right_more.setVisibility(View.VISIBLE);
        return this;
    }

    /**
     * 设置右边更多按扭监听,将使用默认的图片
     */
    public BaseTitleBar setRightMoreLis(OnClickListener lis) {
        ib_right_more.setOnClickListener(lis);
        ib_right_more.setVisibility(View.VISIBLE);
        return this;
    }

    /**
     * 设置左边边按扭可见性 ,为了让标题是居中的，不能设置为gone
     *
     * @param visible
     */
    public BaseTitleBar setLeftVisible(boolean visible){
        tv_left.setVisibility(visible? VISIBLE : GONE);
        return this;
    }

    /**
     * 设置右边更多按扭监听,将使用默认的图片
     */
//    public BaseTitleBar setRightIcon(@DrawableRes int resid, OnClickListener lis) {
//        ib_right_more.setOnClickListener(lis);
//        ib_right_more.setVisibility(View.VISIBLE);
//        ib_right_more.setImageResource(resid);
//        return this;
//    }

    public BaseTitleBar setDividerColor(int color) {
        title_divider.setBackgroundColor(color);
        return this;
    }

    /**
     * @return the tv_title
     */
    public TextView getTitleView() {
        return tv_title;
    }

    public TextView getLeftView() {
        return tv_left;
    }

    public ImageButton getImageMore() {
        return ib_right_more;
    }
}
